package com.onlineshopping.controller;

import com.onlineshopping.entity.ShoppingCar;
import com.onlineshopping.mapper.ShoppingCarMapper;
import com.onlineshopping.mapper.UserMapper;
import com.onlineshopping.utils.JwtTokenUtil;
import com.onlineshopping.vo.ItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author laisir
 * @date 2024/9/24
 */
@RestController
@RequestMapping("/shoppingCar")
public class ShoppingCarController {

    @Autowired
    ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/add")
    public boolean addToCart(@RequestBody ItemVo item, HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
           throw new RuntimeException("Username is null.");
        }
        //System.out.println(item);
        //System.out.println(username);

        shoppingCarMapper.addToCart(username, item);
        return true;
    }

    @GetMapping("/count")
    public Integer getCount(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
            throw new RuntimeException("Username is null.");
        }
        //System.out.println(username);
        return shoppingCarMapper.getCount(username);
    }

    @DeleteMapping("/clear")
    public boolean clear(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
            throw new RuntimeException("Username is null.");
        }
        shoppingCarMapper.clear(username);
        return true;
    }

    @GetMapping("/getAll")
    public List<ShoppingCar> getAll(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
            throw new RuntimeException("Username is null.");
        }
        return shoppingCarMapper.getAll(username);
    }

    @GetMapping("/getOrders")
    public List<ShoppingCar> getOrders(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
            throw new RuntimeException("Username is null.");
        }
        return shoppingCarMapper.getOrders(username);
    }

    @DeleteMapping("/remove")
    public boolean remove(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
            throw new RuntimeException("Username is null.");
        }
        String  itemName = map.get("itemName");
        if (itemName == null) return false;
        //System.out.println(itemName + " " + username);
        return shoppingCarMapper.remove(username, itemName) > 0;
    }

    @PutMapping("/pay")
    public void pay(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        String authToken = authHeader.substring(6);
        String username = JwtTokenUtil.getUserNameFromToken(authToken);
        if (username == null) {
            throw new RuntimeException("Username is null.");
        }
        shoppingCarMapper.pay(username);
    }

    // 新增的接口：获取用户订单历史
    @GetMapping("/getOrdersByUser/{userId}")
    public List<ShoppingCar> getOrdersByUser(@PathVariable Integer userId) {
        String username = userMapper.findById(userId);
        return shoppingCarMapper.getOrders(username);
    }

    @GetMapping("/getAllOrders")
    public List<ShoppingCar> getAllOrders() {
        return shoppingCarMapper.getAllOrders();
    }
}
